package org.lifecycle.model;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

//@Service
public class MyBean implements BeanNameAware,
        BeanFactoryAware,
        ApplicationContextAware,
        InitializingBean,
        DisposableBean {

    public MyBean(){
        System.out.println("Bean对象实例化");
    }
    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware生命周期方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware生命周期方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware生命周期方法");
    }

    @PostConstruct
    public void 初始化方法1(){
        System.out.println("Bean的初始化方法:@PostConstruct");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Bean的初始化方法:InitializingBean");
    }

    public void 初始化方法2(){
        System.out.println("Bean的初始化方法:@Bean(initMethod=本方法名)");
    }

    @PreDestroy
    public void 销毁方法1(){
        System.out.println("Bean的销毁方法：@PreDestroy");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Bean的销毁方法：DisposableBean");
    }

    public void 销毁方法2(){
        System.out.println("Bean的销毁方法：@Bean(destroyMethod=本方法名)");
    }
}
